package model;

import model.Node.NodeState;

public class FieldGenerator {
	public static Node[][] createEmpty() {
		Node[][] nodeNet = new Node[PV.getFieldWidth()][PV.getFieldHeight()];
		for (int i = 0; i < nodeNet.length; i++) {
			for (int j = 0; j < nodeNet[i].length; j++) {
				nodeNet[i][j] = new Node(i, j);
			}
		}
		nodeNet[0][0].setStart(true);
		nodeNet[PV.getFieldWidth()-1][PV.getFieldHeight()-1].setTarget(true);
		//PublicVars.setZoom(580.0f/(PublicVars.getFieldHeight()*PublicVars.getNodeSize()));
		//Pathfinder.getMainframe().getPanelSettings().refresh();
		return nodeNet;
	}
	
	public static void fillMaze(){
		for (int i = 0; i < PV.getNodeNet().length; i++) {
			for (int j = 0; j < PV.getNodeNet()[i].length; j++) {
				//PV.getNodeNet()[i][j] = new Node(i, j);
				PV.getNodeNet()[i][j].setObstacle(true);
				
				PV.getNodeNet()[i][j].setStart(false);
				PV.getNodeNet()[i][j].setTarget(false);
			}
		}
		PV.getNodeNet()[0][0].setObstacle(false);
		PV.getNodeNet()[0][0].setStart(true);
		PV.getNodeNet()[PV.getFieldWidth()-1][PV.getFieldHeight()-1].setObstacle(false);
		PV.getNodeNet()[PV.getFieldWidth()-1][PV.getFieldHeight()-1].setTarget(true);
	}
	public static void createRandomMaze(int percent) {
		Node[][] nodeNet = new Node[PV.getFieldWidth()][PV.getFieldHeight()];
		for (int i = 0; i < PV.getNodeNet().length; i++) {
			for (int j = 0; j < PV.getNodeNet()[i].length; j++) {
				//nodeNet[i][j] = new Node(i, j);
				PV.getNodeNet()[i][j].setStart(false);
				PV.getNodeNet()[i][j].setTarget(false);
				if(Math.random()*100 < percent){
					PV.getNodeNet()[i][j].setObstacle(true);
				}
			}
		}
		PV.getNodeNet()[0][0].setObstacle(false);
		PV.getNodeNet()[0][0].setStart(true);
		PV.getNodeNet()[PV.getFieldWidth()-1][PV.getFieldHeight()-1].setObstacle(false);
		PV.getNodeNet()[PV.getFieldWidth()-1][PV.getFieldHeight()-1].setTarget(true);
		//PublicVars.setZoom(580.0f/(PublicVars.getFieldHeight()*PublicVars.getNodeSize()));
		//Pathfinder.getMainframe().getPanelSettings().refresh();
		//return nodeNet;
	}
	
	public static Node[][] createLogicMaze(int percentFirst, int percentNext) {
		return createLogicMazeInternal(percentFirst, percentNext);
	}
	
	public static void createLogicMaze(int percent) {
		 createLogicMazeInternal(percent/2, percent);
	}
	
	private static Node[][] createLogicMazeInternal(int percentFirst, int percentNext) {
		Node[][] nodeNet = new Node[PV.getFieldWidth()][PV.getFieldHeight()];
		int percent = percentFirst;
		for (int i = 0; i < nodeNet.length; i++) {
			for (int j = 0; j < nodeNet[i].length; j++) {
				boolean hasNext = false;
				PV.getNodeNet()[i][j].setStart(false);
				PV.getNodeNet()[i][j].setTarget(false);
				if(i == 0){
					hasNext = true;
				} else if(i+1 == nodeNet.length){
					hasNext = true;
				} else if(nodeNet[i-1][j].isObstacle()){
					hasNext = true;
				}
				if(j == 0){
					hasNext = true;
				} else if(j+1 == nodeNet[i].length){
					hasNext = true;
				} else if(nodeNet[i][j-1].isObstacle()){
					hasNext = true;
				}
				
				
				if(hasNext){
					percent = percentNext;
				} else {
					percent = percentFirst;
				}
				nodeNet[i][j] = new Node(i, j);
				if(Math.random()*100 < percent){
					PV.getNodeNet()[i][j].setObstacle(true);;
				}
			}
		}
		PV.getNodeNet()[0][0].setObstacle(false);
		PV.getNodeNet()[0][0].setStart(true);
		PV.getNodeNet()[PV.getFieldWidth()-1][PV.getFieldHeight()-1].setObstacle(false);
		PV.getNodeNet()[PV.getFieldWidth()-1][PV.getFieldHeight()-1].setTarget(true);
		//PublicVars.setZoom(580.0f/(PublicVars.getFieldHeight()*PublicVars.getNodeSize()));
		//Pathfinder.getMainframe().getPanelSettings().refresh();
		return nodeNet;
	}
	
	public static void clearNodeNet(boolean withObst){
		for (int i = 0; i < PV.getFieldWidth(); i++) {
			for (int j = 0; j < PV.getFieldHeight(); j++) {
				PV.getNodeNet()[i][j].setCosts(0);
				PV.getNodeNet()[i][j].setgCosts(0);
				PV.getNodeNet()[i][j].sethCosts(0);
				PV.getNodeNet()[i][j].setState(NodeState.UNKNOWN);
				PV.getNodeNet()[i][j].setLast(null);
				if (PV.getNodeNet()[i][j].isStart() || PV.getNodeNet()[i][j].isTarget()) {
					PV.getNodeNet()[i][j].setObstacle(false);
				}
				if(withObst)
				PV.getNodeNet()[i][j].setObstacle(false);
				PV.getNodeNet()[i][j].repaint();
			}
		}
		PV.getNodeNet()[0][0].setObstacle(false);
	}
}
